# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2Json_v2 import Table2JsonByYM
from com.zzh.utils.requestFieldMapDBField import requestFieldMapToDBField_YM
from com.zzh.utils.sqlUtils.SqlUtils import SqlUtils

import sys
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)


import time
localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))


class UserLoginLogDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()


    def queryUserLoginLogById(self, id):
        """
        根据ID查询信息    查询    zzh_user_oauth
        :param :
        :return:
        单体查询，按照id查询
        """
        sql = "SELECT * FROM zzh_user_login_log WHERE id=%s"
        cur = self.conn.cursor()
        total_count = cur.execute(sql,[id])
        if total_count:
            result = cur.fetchall()
            cur.close()
            self.conn.close()
            userOauthJson = Table2JsonByYM.user_login_log_list_to_json(result)
            return userOauthJson, total_count
        else:
            cur.close()
            self.conn.close()
            return None, 0

    def queryUserLoginLogByCondition(self, **kwargs):
        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']
        page_index = kwargs['pageIndex']
        PAGE_SIZE = kwargs['pageSize']
        fieldMap = requestFieldMapToDBField_YM.userLoginLogMap#要改的这个
        whereSentence = SqlUtils.getOption(conditions, fieldMap)
        cur = self.conn.cursor()
        if multiSort:
            # 排序拼接
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT * FROM zzh_user_login_log WHERE " + whereSentence + " ORDER BY " + sortRule
        else:
            sql = "SELECT * FROM zzh_user_login_log WHERE " + whereSentence
        rs = cur.execute(sql)

        total_count = cur.execute(sql)
        if total_count:
            result = cur.fetchall()

            cur.close()
            self.conn.close()
            if (page_index * PAGE_SIZE) < total_count:
                start = (page_index - 1) * PAGE_SIZE
                end = page_index * PAGE_SIZE
                userOauthPart = result[start: end]
                userOauthJSON = Table2JsonByYM.user_login_log_list_to_json(userOauthPart)
                return userOauthJSON,total_count
            else:
                start = (page_index - 1) * PAGE_SIZE
                userOauthPart = result[start: ]
                userOauthJSON = Table2JsonByYM.user_login_log_list_to_json(userOauthPart)
                return userOauthJSON, total_count

        else:
            cur.close()
            self.conn.close()
            return None, 0


    def addUserLoginLog(self,Info):
        """

        :param userRechargeInfo:
        :return:
        """
        userId = Info['userId']  # 必填，请传递user_id
        userName = Info['userName']  #
        loginTime = localTime
        loginIp = Info['loginIp']  #
        remark = Info['remark']
        loginType = Info['loginType']#

        cur = self.conn.cursor()
        sql = 'INSERT INTO zzh_user_login_log (user_id, user_name, login_time, login_ip, remark, login_type) VALUES ( %s, %s ,%s, %s, %s, %s)'
        rs=cur.execute(sql,[userId,userName,loginTime,loginIp,remark,loginType])
        self.conn.commit()
        cur.close()
        self.conn.close()
        if rs:
            return rs
        else:
            return None

    """
    def updateUserLoginLog(self,Info):#无需更新
        id = Info['id']
        userId = Info['userId']  # 必填，请传递user_id
        userName = Info['userName']  # messageName  消息接收人名称
        oauthName = Info['oauthName']  #
        oauthAccessToken = Info['oauthAccessToken']  #
        oauthOpenId = Info['oauthOpenId']  #
        # addTime = localTime   #更新的时候添加时间不允许修改
        cur = self.conn.cursor()
        sql = 'UPDATE zzh_user_login_log SET user_id=%s, user_name=%s, oauth_name=%s, oauth_access_token=%s, oauth_openid=%s WHERE id=%s'
        rs=cur.execute(sql,[userId,userName,oauthName,oauthAccessToken,oauthOpenId,id])
        self.conn.commit()
        cur.close()
        self.conn.close()
        if rs:
            return rs
        else:
            return None

    """


    def deleteUserLoginLog(self, ids):
        """
        根据ID删除数据表信息，ID需要以数组形式传递
        :param adminIdArray: id 构成的数组 []
        :return:
        """
        sql = "delete from zzh_user_login_log WHERE id=%s"
        cur = self.conn.cursor()
        for id in ids:
            rs = cur.execute(sql, [id])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs


    def queryUserGroupByCondition(self, **kwargs):
        """
        条件查询  zzh_user_oauth
        :param :
        :return:
        此处使用userId为条件查询

        groupName = kwargs['groupName']
        page_index = kwargs['pageIndex']

        sql = "SELECT * FROM zzh_user_group WHERE group_name=%s"
        cur = self.conn.cursor()
        total_count = cur.execute(sql,[groupName])
        if total_count:
            result = cur.fetchall()
            cur.close()
            self.conn.close()
            if (page_index * PAGE_SIZE) < total_count:
                start = (page_index - 1) * PAGE_SIZE
                end = page_index * PAGE_SIZE
                userOauthPart = result[start: end]
                userOauthJSON = Table2JsonByYM.user_group_list_to_json(userOauthPart)
                return total_count, userOauthJSON
            else:
                start = (page_index - 1) * PAGE_SIZE
                userOauthPart = result[start: ]
                userOauthJSON = Table2JsonByYM.user_group_list_to_json(userOauthPart)
                return total_count, userOauthJSON

        else:
            cur.close()
            self.conn.close()
            return None
        """
        pass